<%@ page import="schoolsource.sql.SchoolDBSQL" %>
<%@ page import="schoolsource.model.*" %>
<%@ page import="schoolsource.gui.FormFields" %>
<%@ page import="schoolsource.security.SecurityToken" %>
<%@ page import="java.util.logging.*" %>
<%@ page import="schoolsource.util.SchoolLogger" %>

<% SecurityToken st = (SecurityToken) request.getAttribute(FormFields.SESSION); 
   String token = (String) st.getValue(FormFields.TOKEN);
   boolean parent = (st.getValue(FormFields.ISPARENT) != null);
   String name = (String) st.getValue(SchoolDBSQL.USRFNAME) + " " + (String) st.getValue(SchoolDBSQL.USRLNAME);
   ClassContainer classC[] = (ClassContainer[]) st.getValue("classContainer");
   ContactContainer commC[] = (ContactContainer[]) st.getValue("communicationContainer");
   UserContainer studentContainer = (UserContainer) st.getValue("studentContainer");
   UserContainer studentClassTeacher[] = (UserContainer[]) st.getValue("studentClassTeacher");
   UserContainer studentCommTeacher[] = (UserContainer[]) st.getValue("studentCommTeacher");
   int classnum = (new Integer((String) st.getValue(FormFields.CLASSNUM))).intValue();
   int hwnum = (new Integer((String) st.getValue(FormFields.HWNUM))).intValue();
   HomeworkContainer studentHomework[] = (HomeworkContainer[]) st.getValue("homeworkContainer");
   
SchoolLogger.logger.log(Level.INFO, "HomeworkForm");
   SubmissionContainer submission = SubmissionContainer.getSubmissionContainer(classC[classnum].getClassAssignedID().toString(),
                                           studentHomework[hwnum].getHomeworkID().toString(),
                                           studentContainer.getAssignedID().toString(),
                                           (String) st.getValue(FormFields.SCHOOLNAME));
   String loginType = (String) request.getAttribute(FormFields.USRTYPE); 
   String schoolName = (String) request.getAttribute(FormFields.SCHOOLNAME);
   AttachmentContainer attachmentC[];
   boolean allowSubmissions;
   
   allowSubmissions = studentHomework[hwnum].getSubmissionsAllowed().booleanValue();
   if (hwnum > -1) {
        attachmentC = studentHomework[hwnum].getAttachments();
   }
   else {
        attachmentC = new AttachmentContainer[] {};
   }
   GradeContainer studentGrade[] = ((GradeContainer[]) st.getValue("studentGrade")); %>


<HTML>
  <HEAD>
    <SCRIPT>
      function hwConfirm(a){
          confirm(a);
      }
      function postProcessing(action) {
          document.forms[0].<%= FormFields.LINKCLICKED %>.value = action;
          document.forms[0].submit();
      }

    </SCRIPT>
  </HEAD>
  <BODY>
    <FORM NAME="classinfoform" METHOD="POST" >
      <INPUT TYPE="HIDDEN" NAME="<%= FormFields.CURRENT %>" VALUE="<%= FormFields.SUBFRAME %>">
      <INPUT TYPE="HIDDEN" NAME="<%= FormFields.TOKEN %>" VALUE="<%= token %>">
      <INPUT TYPE="HIDDEN" NAME="<%= FormFields.PARAMETER %>" VALUE="">
      <INPUT TYPE="HIDDEN" NAME="<%= FormFields.CLASSNUM %>" VALUE="<%= classnum %>">
      <INPUT TYPE="HIDDEN" NAME="<%= FormFields.LINKCLICKED %>" VALUE="">
      <INPUT TYPE="HIDDEN" NAME="<%= FormFields.SCHOOLNAME %>" VALUE="<%= schoolName %>">
      <INPUT TYPE="HIDDEN" NAME="<%= FormFields.USRTYPE %>" VALUE="<%= loginType %>">
      <INPUT TYPE="HIDDEN" NAME="<%= SchoolDBSQL.HWASSIGNEDID %>" VALUE="<%= studentHomework[hwnum].getHomeworkID().toString() %>">

    <%
//    if (classnum == -2) {
        out.println("<FONT STYLE=\"font-size:12;font-family:sans-serif;\">");
        out.println("<B>");
        out.println(ClassContainer.getClassContainer(studentHomework[hwnum].getClassID().toString(), schoolName).getAssociatedCourse().getCourseShortName().toString() + " ");
        out.println("<BR>&nbsp;<BR>");
        out.println("</B>");
        out.println("</FONT>");
//    }
    %>
    <TABLE STYLE="font-size:11;font-family:sans-serif;">
     <TR>
         <TD WIDTH="49"><B>Homework:&nbsp;&nbsp;</B></TD>
         <TD WIDTH="200"><%= studentHomework[hwnum].getShortName().toString() %></TD>
         <TD><B>Points:&nbsp;&nbsp;</B></TD>
         <TD><%= studentHomework[hwnum].getTotalPoints().toString() %></TD>
     <% if (!studentGrade[hwnum].getGrade().toString().equals("-1.0")) {
            out.println("<TD><B>Score:&nbsp;&nbsp;</B></TD><TD>" + studentGrade[hwnum].getGrade().toString() + "</TD>");
        }
        else {
            out.println("<TD>&nbsp;</TD><TD>&nbsp;</TD>");
        }
      %>
     </TR>
     <TR>
         <TD><B>Date assigned:&nbsp;&nbsp;</B></TD>
         <TD><%= studentHomework[hwnum].getDateAssigned().toString() %></TD>
         <TD><B>Date due:&nbsp;&nbsp;</B></TD>
         <TD><%= studentHomework[hwnum].getDateDue().toString() %></TD>
         <TD>&nbsp;</TD>
         <TD>&nbsp;</TD>
     </TR>
    <%
        if (attachmentC.length != 0) {
            out.println("<TR>");
            out.println("<TD><B>Attachments:&nbsp;&nbsp;</B>" + "</TD><TD COLSPAN=\"3\">");
            for (int i = 0; i < attachmentC.length; i++) {
                out.println("<a href=\"..\\" + attachmentC[i].getFilePath().toString()+ attachmentC[i].getFileName().toString() + "\" target='4087996' onClick=\"window.open('','4087996','width=640,height=480,resizeable=1,status=1,scrollbars=1,menubar=1')\"><IMG src=\"../pics/paperclip.gif\" width=18 border=0> " + attachmentC[i].getFileName().toString() + "  (" + attachmentC[i].getFileSize().toString() + " bytes)</a><br>");
            }
            out.println("</TD></TR>");
        }
        
//        if (allowSubmissions || submission.getUpdateAllowed().booleanValue()) {
        if ( submission.getUpdateAllowed().booleanValue()) {
            out.println("<TR>");
            out.println("<TD><B>Submit Homework:&nbsp;&nbsp;</B></TD>");
            out.println("<TD COLSPAN=\"2\"><input type=\"file\" NAME=\"" + SchoolDBSQL.SUBMISSIONFILENAME + "\" VALUE=\"\"></TD>");
            out.println("<TD><INPUT TYPE=\"BUTTON\" NAME=\"submitbutton\" VALUE=\"Submit Homework\" onClick=\"postProcessing('Submit Homework')\"></TD>");
            out.println("</TR>");
        }
        if (!"".equals(submission.getFileName().toString())) {
            out.println("<TR>");
            out.println("<TD><B>Homework submitted on</B></TD><TD>" + submission.getSubmissionDate().toString() + "</B></TD>");
            out.println("</TR>");
        }
        
        %>
     <TR><TD COLSPAN="6"><HR></TR>
     <TR>
     <TD VALIGN="TOP"><B>Description:&nbsp;&nbsp;</B></TD><TD COLSPAN="3">
       <%
          StringBuffer desc = new StringBuffer(studentHomework[hwnum].getDescription().toString());
          int linebreakIndex = 0;
          for (int i = 0; i < desc.length(); i++) {
              if ((int) desc.charAt(i) == 13) {
                  desc.replace(i, i + 1, "<BR>");
              }
          }
          out.println(desc.toString());
       %>
     </TD>
     </TR>
     <% StringBuffer sbhomework = new StringBuffer(studentHomework[hwnum].getAnswers().toString());
        StringBuffer sbnote = new StringBuffer(studentHomework[hwnum].getNotes().toString());
        if (sbnote != null && !sbnote.toString().equals("")) {
             linebreakIndex = 0;
             for (int i = 0; i < sbnote.length(); i++) {
                 if ((int) sbnote.charAt(i) == 13) {
                     sbnote.replace(i, i + 1, "<BR>");
                 }
             }
             out.println("<TR><TD COLSPAN=\"6\"><HR></TR>");
             out.println("<TR><TD VALIGN=\"TOP\"><B>Notes:&nbsp;&nbsp;</B></TD><TD COLSPAN=\"5\">" + sbnote.toString() + "</TD></TR>");
        } 
        if (sbhomework != null && !sbhomework.toString().equals("")) {
             linebreakIndex = 0;
             for (int i = 0; i < sbhomework.length(); i++) {
                 if ((int) sbhomework.charAt(i) == 13) {
                     sbhomework.replace(i, i + 1, "<BR>");
                 }
             }
             out.println("<TR><TD COLSPAN=\"6\"><HR></TR>");
             out.println("<TR><TD VALIGN=\"TOP\"><B>Answers:&nbsp;&nbsp;</B></TD><TD COLSPAN=\"5\">" + sbhomework + "</TD></TR>");
        }
      %>
     </TABLE>
    </FORM>
  </BODY>
</HTML>